package com.ruoyi.mateWarehousing.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateTest;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.mateWarehousing.domain.Inventory;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.service.MateWarehousingService;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.product.domain.Product;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateWarehousing")
public class MateWarehousingController extends BaseController {
    @Autowired
    MateWarehousingService mateWarehousingService;


    /**
     * 查询产品列表
     */
    @RequiresPermissions("business:warehousing:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "mateProductName", required = false) String mateProductName,
                              @RequestParam(value = "mateProductType", required = false) String mateProductType) {

        MaterialProduct product = new MaterialProduct();
        product.setMateProductName(mateProductName);
        product.setMateProductType(mateProductType);
        product.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        startPage();
        List<MaterialProduct> list = mateWarehousingService.selectProductToWar(product);
        return getDataTable(list);

    }

    /**
     * 库存预测——新版
     * @param inventory
     * @return
     */
    @GetMapping("/list_new")
    @RequiresPermissions("business:warehousing:list")
    @ResponseBody
    public AjaxResult list_new(Inventory inventory) {
        List<Inventory> list = mateWarehousingService.selectProductToWar_new(inventory);
        return AjaxResult.success(list);
    }

    @GetMapping("/selectTendency")
    @ResponseBody
    public Map<String, Object> selectTendency() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateRequistionModel> mateRequistionModelList = mateWarehousingService.selectTendency();
        resMap.put("MateRequistionModel",mateRequistionModelList);
        return resMap;
    }

    /**
     * 查询批号的库存记录
     */
    @GetMapping("/selectWarehousingDetail")
    @ResponseBody
    public TableDataInfo selectWarehousingDetail(@RequestParam(value = "mateproductId", required = false) Long mateproductId,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {

        MateWarehousing warehousing = new MateWarehousing();
        warehousing.setMateproductId(mateproductId);
        warehousing.setDeptId(SecurityUtils.getDeptId());
        warehousing.setMateProductType(mateProductType);
        List<MateWarehousing> list = mateWarehousingService.selectWarehousingDetail(warehousing);
        return getDataTable(list);

    }

    /**
     * 查询库存记录
     */
    @GetMapping("/selectWarehousingRecords")
    @ResponseBody
    public List<WarehousingRecords> selectWarehousingRecords(@RequestParam(value = "warehousingId", required = false) Long warehousingId) {
        startPage();
        WarehousingRecords warehousingRecords = new WarehousingRecords();
        warehousingRecords.setWarehousingId(warehousingId);
        List<WarehousingRecords> list = mateWarehousingService.selectWarehousingRecords(warehousingRecords);
        for (WarehousingRecords warehousingRecords1 : list) {
            if (warehousingRecords1.getAllnum() == 0) {
                warehousingRecords1.setAllnum(warehousingRecords1.getInventoryNum());
            }
        }

        return list;
    }

    /**
     * 查询库存记录
     */
    @GetMapping("/selectMateCheck")
    @ResponseBody
    public Map<String, Object> selectMateCheck(@RequestParam(value = "mateproductId", required = false) Long mateproductId,
                                               @RequestParam(value = "mateBatch", required = false) String mateBatch,
                                               @RequestParam(value = "type", required = false) String type) {

        Map<String, Object> resMap = new HashMap<>();
        MateWarehousing mateWarehousing = new MateWarehousing();
        mateWarehousing.setMateproductId(mateproductId);
        mateWarehousing.setMateBatch(mateBatch);
        mateWarehousing.setDeptId(SecurityUtils.getDeptId());

        if ("0".equals(type)) {
            List<MateCheckModel> mateCheckModels = mateWarehousingService.selectMateCheck(mateWarehousing);
            resMap.put("checkResult", mateCheckModels);

        } else if ("1".equals(type)) {

            List<MateUserDevice> mateUserDevices = mateWarehousingService.selectmateUserDevices(mateWarehousing);
            resMap.put("checkResult", mateUserDevices);

        } else if ("2".equals(type)) {

            List<MateCheckModel> mateCheckModels = mateWarehousingService.selectMateCheck(mateWarehousing);
            resMap.put("checkResult", mateCheckModels);
        }
        return resMap;
    }
}
